package listbyorder.access101_200.test151;

import java.util.Stack;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/13 8:56
 */
public class Solution2 {

    // 300斩解法———— 使用栈结构
    public String reverseWords(String s) {
        Stack<String> stack = new Stack<>();
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                if (temp.length() != 0) {
                    stack.add(temp.toString());
                    temp = new StringBuilder();
                }
            } else {
                temp.append(s.charAt(i));
            }
        }
        if (temp.length() != 0) {
            stack.push(temp.toString());
        }
        if (stack.isEmpty()) {
            return "";
        }
        temp = new StringBuilder();
        temp.append(stack.pop());
        while (!stack.isEmpty()) {
            temp.append(" ");
            temp.append(stack.pop());
        }
        return temp.toString();
    }

}
